<template>
  <div class="article-list">
    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文章标题" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="分类" align="center" prop="categoryName" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" type="info">草稿</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="success">已发布</el-tag>
          <el-tag v-else type="danger">已撤回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="置顶" align="center" prop="isTop">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isTop === '1'" type="success">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="viewCount" />
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button 
            size="mini" 
            type="text" 
            icon="el-icon-view" 
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.status !== '1'"
            size="mini"
            type="text"
            icon="el-icon-upload2"
            @click="handlePublish(scope.row)"
          >发布</el-button>
          <el-button
            v-if="scope.row.status === '1'"
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleWithdraw(scope.row)"
          >撤回</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listArticle, publishArticle, withdrawArticle, delArticle } from '@/api/cms/article'

export default {
  name: 'ArticleList',
  props: {
    categoryId: {
      type: [Number, String],
      default: null
    }
  },
  data() {
    return {
      loading: true,
      articleList: [],
      total: 0,
      ids: [],
      single: true,
      multiple: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: null,
        title: null
      }
    }
  },
  watch: {
    categoryId(newVal) {
      this.queryParams.categoryId = newVal
      this.getList()
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.articleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleView(row) {
      this.$emit('view', row)
    },
    handleUpdate(row) {
      this.$emit('edit', row)
    },
    handlePublish(row) {
      this.$confirm('是否确认发布文章"' + row.title + '"?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => publishArticle(row.articleId)).then(() => {
        this.getList()
        this.msgSuccess('发布成功')
      }).catch(() => {})
    },
    handleWithdraw(row) {
      this.$confirm('是否确认撤回文章"' + row.title + '"?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => withdrawArticle(row.articleId)).then(() => {
        this.getList()
        this.msgSuccess('撤回成功')
      }).catch(() => {})
    },
    handleDelete(row) {
      this.$confirm('是否确认删除文章"' + row.title + '"?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => delArticle(row.articleId)).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    }
  }
}
</script>
